<#assign classNameLower = className?uncap_first>
package ${packageName}.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.shareinfo.framework.pagination.Page;
import com.shareinfo.framework.controller.BaseController;
import com.shareinfo.framework.utils.RichConvertUtils;

import com.shareinfo.permission.model.UserDept;
import com.shareinfo.permission.query.UserDeptQuery;
import com.shareinfo.permission.service.IUserDeptService;

@Controller
@RequestMapping("${urlPath}/${classNameLower}")
public class ${className}Controller extends BaseController<${className}Model>
{
	@Resource(name="${classNameLower}Service")
	private I${className}Service ${classNameLower}Service;

	@RequestMapping(value = "/list.do")
	public @ResponseBody
	Map<String, Object> list(HttpServletRequest request)
	{
		${className}Query query = parseFrom(${className}Query.class, request);
		Page page = new Page(request);

		List<${className}Model> list = ${classNameLower}Service.selectList(query, page);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Page.ROWS, list);
		map.put(Page.TOTAL, page.totalCount);

		return map;
	}

	@RequestMapping(value = "/add.do")
	public ModelAndView add(HttpServletRequest request)
	{
		${className}Model ${classNameLower}Model = parseFrom(${className}Model.class, request);

		ModelAndView view = new ModelAndView("${urlPath}/${classNameLower}/${classNameLower}Add");
		view.addObject(INFO, userDept);
		return view;
	}

	@RequestMapping(value = "/doAdd.do")
	public @ResponseBody
	Map<String, Object> doAdd(HttpServletRequest request)
	{
		${className}Model ${classNameLower}Model = parseFrom(${className}Model.class, request);
		Map<String, Object> map = new HashMap<String, Object>();

		try
		{
			${classNameLower}Service.insert(${classNameLower}Model);
			map.put(RESULT, SUCCESS);
		}
		catch (Exception e)
		{
			map.put(RESULT, FAIL);
			map.put(ERROR, e.getMessage());
		}

		return map;
	}

	@RequestMapping(value = "/update.do")
	public ModelAndView update(HttpServletRequest request)
	{
		${className}Query query = parseFrom(${className}Query.class, request);

		${className}Model ${classNameLower}Model = ${classNameLower}Service.selectOne(query);

		ModelAndView view = new ModelAndView("${urlPath}/${classNameLower}/${classNameLower}Update");
		view.addObject(INFO, userDept);
		return view;
	}

	@RequestMapping(value = "/doUpdate.do")
	public @ResponseBody
	Map<String, Object> doUpdate(HttpServletRequest request)
	{
		${className}Model ${classNameLower}Model = parseFrom(${className}Model.class, request);
		Map<String, Object> map = new HashMap<String, Object>();

		try
		{
			userDeptService.update(${classNameLower}Model);
			map.put(RESULT, SUCCESS);
		}
		catch (Exception e)
		{
			map.put(RESULT, FAIL);
			map.put(ERROR, e.getMessage());
		}

		return map;
	}

	@RequestMapping(value = "/doDelete.do", headers = { "content-type=application/json;charset=UTF-8" })
	public @ResponseBody
	Map<String, Object> doDelete(HttpServletRequest request, @RequestBody List<Map> list)
	{
		List<UserDept> userDeptList = parseFrom(UserDept.class, list);
		List<String> deptIdList = RichConvertUtils.mapListToStringList("deptId", list);

		Map<String, Object> map = new HashMap<String, Object>();

		Integer childCount = userDeptService.selectChildCount(deptIdList);
		if (childCount > 0)
		{
			map.put(RESULT, FAIL);
			map.put(ERROR, "选择的节点存在子节点，请先删除子节点！");
			return map;
		}

		try
		{
			userDeptService.delete(userDeptList);
			map.put(RESULT, SUCCESS);
		}
		catch (Exception e)
		{
			map.put(RESULT, FAIL);
			map.put(ERROR, e.getMessage());
		}

		return map;
	}
}